#include <libgen.h>
#include <stdio.h>

#include "porting/CCmdParser.h"

int main(int argc, char *argv[]) {
	enum ParameterName {
		ADULT,
		MARRIED,
		NAME,
		COUNTRY,
		WORKS,
		HOBBIES,
		FATHER,
		MOTHER
	};

	CCmdParser cCmdParser;
	cCmdParserConstruct(&cCmdParser, basename(argv[0]));

	cCmdParserBindHelpKey(&cCmdParser, "-?");
	cCmdParserBindHelpKey(&cCmdParser, "--help");
	cCmdParserAddBool(&cCmdParser, ADULT, "Adult");
	cCmdParserBindBoolKey(&cCmdParser, ADULT, "-a");
	cCmdParserBindBoolKey(&cCmdParser, ADULT, "--adult");
	cCmdParserAddBool(&cCmdParser, MARRIED, "Married");
	cCmdParserBindBoolKey(&cCmdParser, MARRIED, "-m");
	cCmdParserBindBoolKey(&cCmdParser, MARRIED, "--married");
	cCmdParserAddVariable(&cCmdParser, NAME, "Name", false);
	cCmdParserBindVariableKey(&cCmdParser, NAME, "-n", "hint");
	cCmdParserBindVariableExpression(&cCmdParser, NAME, "--name", "=", "hint", NULL);
	cCmdParserAddVariable(&cCmdParser, COUNTRY, "Country", true);
	cCmdParserBindVariableKey(&cCmdParser, COUNTRY, "-c", "hint");
	cCmdParserBindVariableExpression(&cCmdParser, COUNTRY, "--country", "=", "hint", "china");
	cCmdParserAddList(&cCmdParser, WORKS, "Works", false);
	cCmdParserBindListKey(&cCmdParser, WORKS, "-w", "hint");
	cCmdParserBindListExpression(&cCmdParser, WORKS, "--work", "=", "hint", NULL);
	cCmdParserAddList(&cCmdParser, HOBBIES, "Hobbies", true);
	cCmdParserBindListKey(&cCmdParser, HOBBIES, "-h", "hint");
	cCmdParserBindListExpression(&cCmdParser, HOBBIES, "--hobby", "=", "hint", "sleeping");
	cCmdParserAddOperand(&cCmdParser, FATHER, "Father", "father");
	cCmdParserAddOperand(&cCmdParser, MOTHER, "Mother", "mother");
	cCmdParserEnableExtension(&cCmdParser, "Friends", "friends");

	const char *const arguments[] = {"-?", "--help",
									 "-a", "--adult",
									 "-n", "zm", "--name=zjiemain",
									 "-c", "cn", "--country",
									 "-w", "student", "--work=engineer", "--work=designer",
									 "-h", "eating", "--hobby=playing", "--hobby",
									 "dad", "mom",
									 "linux", "git", "clion"};
	if (cCmdParserParse(&cCmdParser, sizeof(arguments) / sizeof(const char *), arguments)) {
		const char *buf[128];
		printf("%-12s%d\n", "Adult:", cCmdParserGetBool(&cCmdParser, ADULT));
		printf("%-12s%d\n", "Married:", cCmdParserGetBool(&cCmdParser, MARRIED));
		printf("%-12s%s\n", "Name:", cCmdParserGetVariable(&cCmdParser, NAME));
		printf("%-12s%s\n", "Country:", cCmdParserGetVariable(&cCmdParser, COUNTRY));
		printf("%-12s", "Works:");
		cCmdParserGetList(&cCmdParser, WORKS, buf, sizeof(buf));
		for (int i = 0; i < sizeof(buf) && buf[i] != NULL; ++i) {
			if (i != 0) {
				printf("%s", " ");
			}
			printf("%s", buf[i]);
		}
		printf("%s", "\n");
		printf("%-12s", "Hobbies:");
		cCmdParserGetList(&cCmdParser, HOBBIES, buf, sizeof(buf));
		for (int i = 0; i < sizeof(buf) && buf[i] != NULL; ++i) {
			if (i != 0) {
				printf("%s", " ");
			}
			printf("%s", buf[i]);
		}
		printf("%s", "\n");
		printf("%-12s%s\n", "Father:", cCmdParserGetOperand(&cCmdParser, FATHER));
		printf("%-12s%s\n", "Mother:", cCmdParserGetOperand(&cCmdParser, MOTHER));
		printf("%-12s", "Friends:");
		cCmdParserGetExtension(&cCmdParser, buf, sizeof(buf));
		for (int i = 0; i < sizeof(buf) && buf[i] != NULL; ++i) {
			if (i != 0) {
				printf("%s", " ");
			}
			printf("%s", buf[i]);
		}
		printf("%s", "\n");
	}
	else {
		printf("[ERR] (%s, %d) %s\n", __FILE__, __LINE__, "info");
	}

	cCmdParserDestruct(&cCmdParser);

	return 0;
}
